package com.youdoneed.news.news.web;

import com.youdoneed.base.common.JWTUtils;
import com.youdoneed.news.common.vo.ResultMessage;
import com.youdoneed.news.news.service.CmfAuthorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/Authors", method = {RequestMethod.GET, RequestMethod.POST})
@Api(value = "新闻发布来源模块控制器", description = "新闻发布来源模块控制器")
public class CmfAuthorController {

    private static final Logger LOGGER = LoggerFactory.getLogger(CmfAuthorController.class);

    @Autowired
    CmfAuthorService cmfAuthorService;

    /**
     * 用户关注新闻号
     *
     * @param authorId
     * @return
     * @author hexz
     * @date 2017.6.29
     */
    @RequestMapping(value = "/addAuthorsFocus", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "用户关注新闻号", value = "用户关注新闻号")
    public ResultMessage<Integer> addAuthorsFocus(@ApiParam("新闻作者id") @RequestParam Integer authorId, @ApiParam("客户端请求头附带参数") @RequestHeader("Authorization") String token) {
        String userId = JWTUtils.getTokenUserId(token);
        if (null == authorId || null == userId) {
            return ResultMessage.newFailure("新闻作者id和用户id不能为空");
        }
        //添加关注记录
        cmfAuthorService.addAuthorsFocus(authorId, userId);
        //更新关注数
        cmfAuthorService.updateFocusCount(authorId, "0");
        return new ResultMessage().newSuccess("用户关注新闻号成功");
    }

    /**
     * 用户取消关注新闻号
     *
     * @param authorId
     * @return
     * @author hexz
     * @date 2017.6.29
     */
    @RequestMapping(value = "/cancelAuthorsFocus", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "用户取消关注新闻号", value = "用户取消关注新闻号")
    public ResultMessage cancelAuthorsFocus(@ApiParam("新闻作者id") @RequestParam Integer authorId, @ApiParam("客户端请求头附带参数") @RequestHeader("Authorization") String token) {
        String userId = JWTUtils.getTokenUserId(token);
        if (null == authorId || null == userId) {
            return ResultMessage.newFailure("新闻作者id和用户id不能为空");
        }
        //判断是否已经关注，删除关注记录
        cmfAuthorService.cancelAuthorsFocus(authorId, userId);
        //更新关注数
        cmfAuthorService.updateFocusCount(authorId, "1");
        return new ResultMessage().newSuccess("用户取消关注号成功");
    }

}
